// ©2012 copyright for hustskyking HUST EI

var outputPanel,json,words;
//-----------------------------------创建输出面板--------------------------//
//创建输出面板
function createOutputPanel(){
	var x, y, e;
	e = window.event ? window.event : e;
	x = e.clientX;
	y = e.clientY;
	outputPanel = document.createElement("div");	
	//设置面板属性
	with(outputPanel.style){
		position = "absolute";	
		top = y + "px";
		left = x + "px";
		height = "200px";
		width = "300px";
		overflowY = "scroll";
		background = "#fec";
	}
	outputPanel.id = "panel";
	outputPanel.innerHTML = "";
	//加载面板到页面
	document.body.insertBefore(outputPanel,document.body.childNodes[0]);
}
//----------------------------------有道接口函数创建----------------------------//
//接口返回json代码
function createXmlHttp(){
	if ( window.XMLHttpRequest ){
		//FireFox、Opera等浏览器支持的创建方式
		xmlHttp = new XMLHttpRequest();                 
    } else {
		//IE浏览器支持的创建方式
        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
}
//直接通过XMLHttpRequest对象获取远程网页源代码,参考http://zhidao.baidu.com/question/127027459.html
function getSource(url) {
	 //创建XMLHttpRequest对象
    createXmlHttp();            
	 //设置回调函数                               
    xmlHttp.onreadystatechange = readSource;                  
    xmlHttp.open("GET", url, true);
    xmlHttp.send(null);
}
//将远程网页源代码写入页面文字区域
function readSource() {
    if ( xmlHttp.readyState == 4 ) {
        json = xmlHttp.responseText;
    }
}

//---------------------------------取词、翻译---------------------------------------//
//得到选择的单词,并筛选得到的单词，不符合筛选条件，则不予翻译
function getWords(){
	var text;
	//for IE
	if(navigator.appName == "Microsoft Internet Explorer"){
		text = document.selection.createRange().text;
	}else{
		//For FF
		text = window.getSelection();
	}
	document.getElementById("wordSelected").value = text;
}
//若通过筛选，则进入有道翻译
function dealWords(){
	//有道接口URL 
	words = document.getElementById("wordSelected").value;
	url = "http://fanyi.youdao.com/openapi.do?keyfrom=hustskyking&key=1344144458&type=data&doctype=json&version=1.1&q=" + words;
	//远程获取接口返回json代码并解析
	getSource(url);

}

/*****************************************************
*处理得到的json内容
*经过我的观察，发现有道返回的结果是一个二维数组，一位属性分别是
*translation basic  query  errorCode web
*二位属性只有basic和web有：
*分别是：explains value
******************************************************/

function dealJsonArray(){
	if ( null == dealWords())
	{ 
		return ;
	}else{
		json = eval(json);
	}
	var translation,query,errorCode;
	var basic = new Array();
	var web = new Array();
	//处理和分解json
	translation = json["translation"];
	basic = json["basic"]["explains"];
	query = json["query"];
	errorCode = json["errorCode"];
	web = json["web"]["value"];
	//检验一下	
	//alert(web);
}

//----------------------------------将翻译的结果导入至输出面板中-------------------------//
function output(){
	createOutputPanel();
	dealJsonArray();
	outputPanel.innerHTML = json;
}

//-------------------------------------删除开始创建的面板-------------------------------//
function deletePanel(){
	document.body.removeChild(document.body.childNodes[0]);
}
